(function(){
	var portal = function(){
		this.bodyObj = $("body");

		this.ajaxKey = {
			online : {
				qdygl : "image"
			},
			grant : {
				lbgl : "top",
				cdglbanner : "medium",
				cdglleft : "left",
				cdglright : "right",
				cdglfoot : "bottom"
			},
			aftergrant : {
				aftergrantlbgl : "top"
			},
			articleImg : {
				articleImglbgl : "bottom"
			}
		};

		this.createForm = function(){
			var formObj = $('<form method="post" action="/admin/Advert/upload" target="frameFile" enctype="multipart/form-data" style="display:none;" id="uploadImgObj"></form>');
			var html = ['<input type="file" name="image_name" id="imgFileObj" onchange="portalObj.uploadImg(this)">'];
			html.push('<input type="input" name="image_id" id="imgIdObj" value="">');
			html.push('<input type="input" name="image_width" id="imgWidthObj" value="">');
			html.push('<input type="input" name="image_height" id="imgHeightObj" value="">');
			formObj.html(html.join(""));
			this.bodyObj.append(formObj);
		}

		this.createIframe = function(){
			var iframeObj = $("<iframe id='frameFile' name='frameFile' style='display: none;' onload='(function(obj){if(typeof(portalObj)!==\"undefined\")portalObj.uploadImgCallBack(obj)})(this);'></iframe>");
			this.bodyObj.append(iframeObj);
		}

		this.createForm();
		this.createIframe();
	};

	$(function(){
		window.portalObj = new portal();
	});

	portal.prototype.showSet = function(obj){
		var thisName = $(obj).attr("data-name");
		var setObj = $("div[data-setName='"+thisName+"']");
		setObj.show();
		setObj.siblings('div').hide();
	}

	portal.prototype.showuploadImg = function(obj){
		var imgSize = $(obj).attr("data-size").split(",");
		var id = Math.random().toFixed(6);
		$("#imgWidthObj").val(imgSize[0]);
		$("#imgHeightObj").val(imgSize[1]);
		$("#imgIdObj").val(id);
		$(obj).attr("data-imgId",id);
		$("#imgFileObj").click();
	}

	portal.prototype.checkImgFile = function(obj){
		var thisVal = obj.value;
		if(!thisVal)return false;
		var i=thisVal.lastIndexOf('.');
		var len=thisVal.length;
		var extEndName=thisVal.substring(i+1,len);
		var extName="PNG,JPEG,JPG";//首先对格式进行验证
		if(extName.indexOf(extEndName.toUpperCase())==-1){
			alert("只能上传"+extName+"格式的文件");
			return false;
		}
		return true;
	}

	portal.prototype.uploadImg = function(obj){
		if(!this.checkImgFile(obj)){
			return false;
		}
		$("#uploadImgObj").submit();
		$("#imgFileObj").val("");
	}

	portal.prototype.uploadImgCallBack = function(obj){
		var msg = $(obj.contentWindow.document.body).text();
		var data = eval("("+msg+")");
		if(data.code!=="0000"){
			alert(data.msg);
			return false;
		}

		var imgUrl = data.return_info.img_url;
		var aObj = $("a[data-imgId='"+data.image_id+"']");
		aObj.children('img').remove();
		if(aObj.length)aObj.append('<img src="'+imgUrl+'">');
	}

	portal.prototype.previewSet = function(obj,type){
		var parentObj = $(obj).parent().parent();

		var imgObj = parentObj.find(".imgObj img");
		var titleObj =parentObj.find(".titleObj");
		var urlObj =parentObj.find(".urlObj");
		var wzObj = parentObj.find(".imgIndexObj");
		var partObj = parentObj.find('.partObj');
		var appUrlObj = parentObj.find('.appUrlObj'); // app 跳转链接 4
		var appTypeObj = parentObj.find('.appTypeObj'); // app 跳转链接类型 5

		var imgVal = imgObj.length ? imgObj.attr("src") : "";
		var titleVal = titleObj.val();
		var urlVal = urlObj.val();
		var wzVal = wzObj.length ? wzObj.val() : 0;
		var part_id = typeof partObj.val() != 'undefined' ? partObj.val() : '';
		var appUrlVal = typeof appUrlObj.val() != 'undefined' ? appUrlObj.val() : '';
		var appTypeVal = typeof appTypeObj.val() != 'undefined' ? appTypeObj.val() : '';

		if(!imgVal){
			alert("请上传一张图片");
			return false;
		}
		// if(!titleVal){
		// 	alert("请添加title");
		// 	return false;
		// }
		// if(!urlVal){
		// 	alert("请添加图片跳转页面地址");
		// 	return false;
		// }
		if(titleVal.length>8){
			alert("标题最多为8个汉字");
			return false;
		}
		if(wzVal && isNaN(wzVal)){
			alert("位置编号必须为数字");
			return false;
		}

		var thisV = $(obj).attr("data-val");
		var thisI = $(obj).attr("data-i");
		thisI = thisI ? thisI : 0;
		var moduleObj = $('div[data-name="'+thisV+'"]');
		var moduleData = moduleObj.attr("data-setVal");
		var moduleDataArr = moduleData ? moduleData.split("|") : [];
		moduleDataArr[thisI] = [imgVal,titleVal,urlVal,wzVal,part_id,appUrlVal,appTypeVal].join(",");
		moduleObj.attr("data-setVal",moduleDataArr.join("|"));
		var imgObj = moduleObj.children('img');
		var titleConObj = moduleObj.children('.titleObj');
		if(titleConObj.length){
			titleConObj.html(titleVal);
		}
		imgObj.next('div').hide();
		imgObj.attr("src",imgVal);
		if(thisV.indexOf("lbgl")<0)return false;
		$("#CarouselOff"+type+"Obj a").removeClass('on');
		$("#CarouselOff"+type+"Obj a").filter("[data-i='"+thisI+"']").addClass('on');
	}

	portal.prototype.clearSet = function(obj,type){
		var parentObj = $(obj).parent().parent();

		var imgObj = parentObj.find(".imgObj img");
		var titleObj =parentObj.find(".titleObj");
		var urlObj =parentObj.find(".urlObj");
		var wzObj = parentObj.find(".imgIndexObj");

		if(imgObj.length)imgObj.remove();
		titleObj.val("");
		urlObj.val("");
		if(wzObj.length)wzObj.val("");

		var thisV = $(obj).attr("data-val");
		var thisI = $(obj).attr("data-i");
		thisI = thisI ? thisI : 0;
		var moduleObj = $('div[data-name="'+thisV+'"]');
		var moduleData = moduleObj.attr("data-setVal");
		var moduleDataArr = moduleData ? moduleData.split("|") : [];
		moduleDataArr[thisI] = "";
		moduleObj.attr("data-setVal",moduleDataArr.join("|"));
		var imgObj = moduleObj.children('img');
		imgObj.next('div').show();
		if(thisV.indexOf("lbgl")<0)return false;
		$("#CarouselOff"+type+"Obj a").removeClass('on');
		$("#CarouselOff"+type+"Obj a").filter("[data-i='"+thisI+"']").addClass('on');
	}

	portal.prototype.deleteSet = function(obj,type){
		var CarouselOffListObj = $("#CarouselOff"+type+"Obj a");
		// if(CarouselOffListObj.length==1){
		// 	alert("至少保留一个");
		// 	return false;
		// }

		var clearObj = $(obj).prev();
		this.clearSet(clearObj);

		$(obj).parent().parent().remove();

		var thisI = $(obj).attr("data-i");
		CarouselOffListObj.filter("[data-i='"+thisI+"']").remove();
		CarouselOffListObj.removeClass('on');
		$("#CarouselOff"+type+"Obj a").eq(0).addClass('on');

		var thisV = $(obj).attr("data-val");
		var moduleObj = $('div[data-name="'+thisV+'"]');
		var moduleData = moduleObj.attr("data-setVal");
		var moduleDataArr = moduleData ? moduleData.split("|") : [];
		var thisNI = $("#CarouselOff"+type+"Obj a").eq(0).attr("data-i");
		if(moduleDataArr[thisNI]){
			var imgObj = moduleObj.children('img');
			imgObj[0].src = moduleDataArr[thisNI].split(",")[0];
			imgObj.next('div').hide();
		}else{
			var imgObj = moduleObj.children('img');
			imgObj.next('div').show();
		}
		if($("#CarouselOff"+type+"Obj a").length==0){
			var imgObj = moduleObj.children('img');
			imgObj[0].src = "/Public/images/banner-min.jpg";
			imgObj.next('div').hide();
		}
	}

	portal.prototype.addCarousel = function(type){
		var CarouselOffObj = $("#CarouselOff"+type+"Obj");
		var CarouselOffList = CarouselOffObj.children('a');
		var maxI = 0;
		for(var i=0,ilen=CarouselOffList.length;i<ilen;i++){
			var thisI = Number(CarouselOffList.eq(i).attr("data-i"));
			if(Number(thisI)>maxI)maxI=thisI;
		}
		CarouselOffObj.append('<a href="javascript:void(0)" onclick="portalObj.showCarouseIndex(this)" data-i="'+(maxI+1)+'" class="dot"></a>');
		if(type != 'articleImg'){
			var html = '<div class="item i-banner"><div class="clearfix mb15"><div class="fl mr20"><a href="javascript:void(0);" class="item-img item-up imgObj" onclick="portalObj.showuploadImg(this);" data-size="640,378"><span class="addwrap"><em class="addbtn">+</em><em class="font16">图片'+(maxI+2)+'</em></span></a></div><div class="fl item-rt"><div><span class="mr10">位置:</span><input class="item-ipt mb15 focus imgIndexObj" type="text" value="" placeholder="请输入图片位置"></div><div><span class="mr10">标题:</span><input class="item-ipt mb15 focus titleObj" type="text" value="" placeholder="请输入图片标题"></div><div><span class="mr10">地址:</span><input class="item-ipt urlObj" type="text" value="" placeholder="请输入图片跳转页面地址"></div></div></div><div class="clearfix"><a href="javascript:void(0);" class="pro-btn sm" onclick="portalObj.previewSet(this,\''+type+'\');" data-val="'+type+'lbgl" data-i="'+(maxI+1)+'">提 交</a><a href="javascript:void(0);" class="ml20 fontblue" onclick="portalObj.clearSet(this,\''+type+'\');" data-val="'+type+'lbgl" data-i="'+(maxI+1)+'">清空</a><a href="javascript:void(0);" class="ml20 fontblue" onclick="portalObj.deleteSet(this,\''+type+'\');" data-val="'+type+'lbgl" data-i="'+(maxI+1)+'">删除本条</a></div></div>';
		}else{
			var html = '<div class="item i-banner"><div class="clearfix mb15"><div class="fl mr20"><a href="javascript:void(0);" class="item-img item-up imgObj" onclick="portalObj.showuploadImg(this);" data-size="640,378"><span class="addwrap"><em class="addbtn">+</em><em class="font16">图片'+(maxI+2)+'</em></span></a></div><div class="fl item-rt"><div><span class="mr47">标题:</span><input class="item-ipt mb15 focus titleObj" type="text" value="" placeholder="请输入图片标题"></div><div><span class="mr47">地址:</span><input class="item-ipt mb15 focus urlObj" type="text" value="" placeholder="请输入图片跳转页面地址"></div><div><span class="mr10">客户端类型:</span><select class="item-sel mb15 focus appTypeObj"><option value = "in">内部链接</option><option value = "out">外部链接</option></select></div><div><span class="mr10">客户端地址:</span><input class="item-ipt appUrlObj" type="text" value="" placeholder="请输入客户端图片跳转页面地址"></div></div></div><div class="clearfix"><a href="javascript:void(0);" class="pro-btn sm" onclick="portalObj.previewSet(this,\''+type+'\');" data-val="'+type+'lbgl" data-i="'+(maxI+1)+'">提 交</a><a href="javascript:void(0);" class="ml20 fontblue" onclick="portalObj.clearSet(this,\''+type+'\');" data-val="'+type+'lbgl" data-i="'+(maxI+1)+'">清空</a><a href="javascript:void(0);" class="ml20 fontblue" onclick="portalObj.deleteSet(this,\''+type+'\');" data-val="'+type+'lbgl" data-i="'+(maxI+1)+'">删除本条</a></div></div>';
		}
		$("#CarouselList"+type+"Obj").append(html);
	}

	portal.prototype.showCarouseIndex = function(obj){
		$(obj).siblings().removeClass('on');
		$(obj).addClass('on');
		var thisI = $(obj).attr("data-i");
		var showSetObj = $(obj).parent().parent().parent();
		var data = showSetObj.attr("data-setval");
		var dataArr = data ? data.split("|") : [];

		if(!dataArr[thisI]){
			var imgObj = showSetObj.children('img');
			imgObj.next('div').show();
			return false;
		}

		var imgObj = showSetObj.children('img');
		imgObj[0].src = dataArr[thisI].split(",")[0];
		imgObj.next('div').hide();
	}


	portal.prototype.submitSet = function(obj){
		var parentObj = $(obj).parent().parent();
		var dataObj = parentObj.find('div[data-name]');

		var postData = new Array();
		for(var i=0,ilen=dataObj.length;i<ilen;i++){
			var thisKey = dataObj.eq(i).attr("data-name");
			var thisData = dataObj.eq(i).attr("data-setval");
			if(!thisData)continue;
			if(!thisKey)continue;
			var data = new Array();
			var thisDataArr = thisData.split("|");
			for(var k=0,klen=thisDataArr.length;k<klen;k++){
				if(!thisDataArr[k])continue;
				var thisDataVal = thisDataArr[k].split(",");
				data.push('{\"title\":\"'+thisDataVal[1]+'\",\"link_url\":\"'+thisDataVal[2]+'\",\"imgurl\":\"'+thisDataVal[0]+'\",\"list_order\":\"'+thisDataVal[3]+'\", "app_url":\"'+thisDataVal[5]+'\", "app_url_type":\"'+thisDataVal[6]+'\"}');
			}

			postData.push('{\"position_type\" : \"'+this.ajaxKey[img_type][thisKey]+'\",\"imglist\":['+data.join(",")+']}');
		}
		if(!confirm("是否要提交？"))return false;
		$.ajax({
			url : "/admin/Advert/introduce",
			data : "img_type="+img_type+"&data="+encodeURIComponent("["+postData.join(",")+"]"),
			type : "post",
			datatype : "json",
			success : function(msg){
				if(msg.code!="0000"){
					alert(msg.msg);
					return false;
				}
				alert("提交成功");
			}
		});
	}
})();